﻿// ***********************************************************************
// Assembly         : Admin.Framework.Core.Repository
// Author           : xwltz
// Created          : 2019-05-25
//
// Last Modified By : xwltz
// Last Modified On : 2019-05-25
// ***********************************************************************
// <summary>
//  基础仓储，主要实现一些常用的DB操作，简化Entity操作
// </summary>
// ***********************************************************************

using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using Admin.Framework.Core.Repository.Interface;
using SqlSugar;

namespace Admin.Framework.Core.Repository
{
    public class BaseRepository<TEntity> : IRepository<TEntity> where TEntity : Domain.Core.BaseEntity, new()
    {
        private readonly DataBaseContext _context = new DataBaseContext();

        public SimpleClient<TEntity> CurrentDb()
        {
            return new SimpleClient<TEntity>(_context.Db);
        }

        public virtual TEntity GetSingle(Expression<Func<TEntity, bool>> exp = null)
        {
            var model = _context.Db.Queryable<TEntity>().Single(exp);
            return model;
        }

        public virtual List<TEntity> GetList()
        {
            var list = _context.Db.Queryable<TEntity>().ToList();
            return list;
        }

        public virtual List<TEntity> GetList(Expression<Func<TEntity, bool>> exp)
        {
            var list = _context.Db.Queryable<TEntity>().Where(exp).ToList(); ;
            return list;
        }

        public virtual bool Add(TEntity entity)
        {
            return new SimpleClient<TEntity>(_context.Db).Insert(entity);
        }

        public virtual bool Update(TEntity entity)
        {
            return new SimpleClient<TEntity>(_context.Db).Update(entity);
        }

        public virtual bool Delete(TEntity entity)
        {
            return new SimpleClient<TEntity>(_context.Db).Delete(entity);
        }
    }
}
